package GUI;


import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.DefaultComboBoxModel;

import client.ClientConsole;

import Library.History;
import Library.ListOrderBookStudent;


public class Gui_ShowReaderCard extends JPanel {

	private JTextField textField;
	private Gui_ShowReaderCard erc;
	private GUI_userLoans us;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;
	private JComboBox readerBOX = null;
	private JComboBox statusBox=null;
	private ClientConsole clc;
	private GUI_LogIn gl;
	private JPanel backPanel;
	private static int flag=1;
	private Gui_ShowReaderCard src = this;
	/**
	 * @wbp.parser.constructor
	 */
	public Gui_ShowReaderCard(ClientConsole cc, final GUI_LogIn gl,final GUI_ManagerMenu mm)
	{
		backPanel=mm;
		clc=cc;
		this.gl=gl;	
	
		same();
	}
	
	public Gui_ShowReaderCard(ClientConsole cc, final GUI_LogIn gl,final GUI_LibrarianMenu lm) {
		
		backPanel=lm;
		clc=cc;
		this.gl=gl;
		same();
	}
		
		public void same()
		{
		erc=this;
		setLayout(null);
		this.setSize(600, 600);
		JLabel lblReaderCardFill = new JLabel("Reader card fill");
		lblReaderCardFill.setFont(new Font("Tahoma", Font.PLAIN, 24));
		lblReaderCardFill.setBounds(178, 11, 173, 23);
		add(lblReaderCardFill);
		
		
		JLabel lblReaderId = new JLabel("Reader Id");
		lblReaderId.setBounds(39, 114, 66, 14);
		add(lblReaderId);
		
		textField = new JTextField();
		textField.setBounds(125, 108, 140, 20);
		add(textField);
		textField.setColumns(10);
		textField.setEditable(false);
		
		JLabel lblUserName = new JLabel("user name");
		lblUserName.setBounds(39, 139, 66, 14);
		add(lblUserName);
		
		textField_1 = new JTextField();
		textField_1.setBounds(125, 136, 140, 20);
		add(textField_1);
		textField_1.setColumns(10);
		textField_1.setEditable(false);
		
		JLabel lblPhone = new JLabel("phone");
		lblPhone.setBounds(39, 170, 46, 14);
		add(lblPhone);
		
		textField_2 = new JTextField();
		textField_2.setBounds(125, 167, 140, 20);
		add(textField_2);
		textField_2.setColumns(10);
		textField_2.setEditable(false);
		
		JLabel lblNewLabel = new JLabel("E-mail");
		lblNewLabel.setBounds(39, 201, 66, 14);
		add(lblNewLabel);
		
		textField_3 = new JTextField();
		textField_3.setBounds(125, 198, 140, 20);
		add(textField_3);
		textField_3.setColumns(10);
		textField_3.setEditable(false);
		
		textField_4 = new JTextField();
		textField_4.setBounds(125, 229, 140, 20);
		add(textField_4);
		textField_4.setColumns(10);
		textField_4.setEditable(false);
		
		JLabel lblLastName = new JLabel("First Name");
		lblLastName.setBounds(39, 232, 66, 14);
		add(lblLastName);
		
		textField_5 = new JTextField();
		textField_5.setBounds(125, 254, 140, 20);
		add(textField_5);
		textField_5.setColumns(10);
		textField_5.setEditable(false);
		
		JLabel lblEmail = new JLabel("Last Name");
		lblEmail.setBounds(39, 257, 46, 14);
		add(lblEmail);

		
	
		
	     JButton btnBack = new JButton("Back");
			btnBack.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					gl.setContentPane(backPanel);
				}
			})
			;
			btnBack.setBounds(262, 330, 89, 23);
			add(btnBack);
		
		JLabel lblChooseReader = new JLabel("Choose Reader:");
		lblChooseReader.setBounds(39, 64, 101, 14);
		add(lblChooseReader);
		
		
		JLabel lblStatus = new JLabel("Status");
		lblStatus.setBounds(39, 282, 46, 14);
		add(lblStatus);
		
		statusBox = new JComboBox();
		statusBox.setModel(new DefaultComboBoxModel(new String[] {"active", "not active", "freeze", "freezeUntil"}));
		statusBox.setBounds(125, 285, 140, 20);
		add(statusBox);
		statusBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if(e.getStateChange()==1)
				{
				String change=e.getItem().toString();
				if(flag==1)
				{
					if(change.equals("not active"))
					{
						try {
							String s[]={"checkForLoanedBooks",textField.getText()};
							clc.client.sendToServer(s);
							while(clc.client.flag==0) System.out.println("");
							clc.client.flag=0;
							int res=(Integer)clc.client.message;
							if(res==1)
							{
								String s2[]={"changeReaderCardStatus",textField.getText(),"freezeUntil"};
								clc.client.sendToServer(s2);
								while(clc.client.flag==0) System.out.print("");
								clc.client.flag=0;
								JOptionPane.showMessageDialog(new JFrame(),"Can not deactivate reader card - there are loand books for this card.\nReader card is now frozen");
							}
							else
							{
								String s2[]={"changeReaderCardStatus",textField.getText(),"not active"};
								clc.client.sendToServer(s2);
								while(clc.client.flag==0) System.out.print("");
								clc.client.flag=0;
								JOptionPane.showMessageDialog(new JFrame(),clc.client.message);
							}
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
					else
					{System.out.println("here");
						String s2[]={"changeReaderCardStatus",textField.getText(),change};
						try {
							clc.client.sendToServer(s2);
							while(clc.client.flag==0) System.out.print("");
							clc.client.flag=0;
							JOptionPane.showMessageDialog(new JFrame(),"Reader card status is now "+change);
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
				}
			}
			}
		});
		/*statusBox.addItem("active");
		statusBox.addItem("not active");
		statusBox.addItem("freeze");
		statusBox.addItem("freezeUntil");*/
		if(clc.rank!=2)
			statusBox.setEnabled(false);
		
		try {
			String[] s={"get readers"};
			clc.client.sendToServer(s);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		while(clc.client.flag==0)
			System.out.print("");
		clc.client.flag=0;
		
		ArrayList<String[]> result;
		result=clc.client.details;
		Iterator i=result.iterator();
		readerBOX = new JComboBox();
		while(i.hasNext())
		{
		String[] res=(String[]) i.next();
		readerBOX.addItem(res[0]);
		}
		readerBOX.setBounds(139, 61, 164, 20);  
		add(readerBOX);
		readerBOX.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				String change=e.getItem().toString();
				String[] s={"get reader card details",change};
				try {
					
					clc.client.sendToServer(s);
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				while(clc.client.flag==0)
					System.out.print("");
				clc.client.flag=0;
				showResults();
				flag=1;
			}
		});
		
		JButton btnShowActiveLoans = new JButton("show active loans");
		btnShowActiveLoans.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				 us=new GUI_userLoans(clc,gl,erc,textField.getText());
				 gl.setContentPane(us);
			}
		});
		btnShowActiveLoans.setBounds(366, 60, 157, 23);
		add(btnShowActiveLoans);
		
		JButton btnLateReturn = new JButton("show Late Return");
		btnLateReturn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				 String readerId=textField.getText();
				   String userName = null; 
				   String s[]={"getuserName",readerId};
					try {
						clc.client.sendToServer(s);
						while(clc.client.flag==0)
						    System.out.print("");
						    clc.client.flag=0;
							userName =  (String)clc.client.message;
				  	} catch (IOException e) {
						e.printStackTrace();
					}	// send message to server
				String str[]={"LateReturn",userName};
				try {
					clc.client.sendToServer(str);
					while(clc.client.flag==0)
					    System.out.print("");
					    clc.client.flag=0;
					    History history =(History)clc.client.message;
					    if(history.historyList.isEmpty())
								JOptionPane.showMessageDialog(new JFrame(),"No delays being recalled");
					    else
						    {
						    	GUI_LendingHistoryStudent lhs= new GUI_LendingHistoryStudent(clc,history.historyList,src);
			     		    	clc.cgui.setContentPane(lhs);
						    }
					  
					    	
				} catch (IOException e) {
					e.printStackTrace();
				}	// send message to server

			}});
			setLayout(null);
			btnLateReturn.setBounds(366, 120, 157, 23);
			this.add(btnLateReturn);
		  
			JButton btnOrderBook  = new JButton("show active Orders");
		   btnOrderBook.addActionListener(new ActionListener() {
			   public void actionPerformed(ActionEvent arg0) {
				   String readerId=textField.getText();
				   String userName = null; 
				   String s[]={"getuserName",readerId};
					try {
						clc.client.sendToServer(s);
						while(clc.client.flag==0)
						    System.out.print("");
						    clc.client.flag=0;
							userName =  (String)clc.client.message;
				  	} catch (IOException e) {
						e.printStackTrace();
					}	// send message to server
					
				  
				  String str1[]={"getListOrderBook",userName};
					try {
						clc.client.sendToServer(str1);
						while(clc.client.flag==0)
						    System.out.print("");
						    clc.client.flag=0;
						    ListOrderBookStudent listOrderBookStudent =(ListOrderBookStudent)clc.client.message;
						    if(listOrderBookStudent.ListOrderBookStudent.isEmpty())
						    {
								JOptionPane.showMessageDialog(new JFrame(),"There is not orders by this user");	
						    }
						    else
						    {
						    	GUI_ShowOrderBook sob= new GUI_ShowOrderBook(clc,listOrderBookStudent.ListOrderBookStudent,src);
						    	clc.cgui.setContentPane(sob);
						    }
						    	
					} catch (IOException e) {
						e.printStackTrace();
					}	// send message to server
					
				 
			}});
		setLayout(null);
		btnOrderBook.setBounds(366, 90, 157, 23);
		this.add(btnOrderBook);
		
				
		JButton btnLogout = new JButton("Log Out");
		btnLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				gl.LogOut();
			}
		});
		btnLogout.setBounds(372, 330, 89, 23);
		add(btnLogout);
		
		init();
	}
	
	private void init() {
		
		String[] s={"get reader card details",readerBOX.getSelectedItem().toString()};
		try {
			
			clc.client.sendToServer(s);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		while(clc.client.flag==0)
			System.out.print("");
		clc.client.flag=0;
		showResults();

			
		}
	public void showResults()
	{
		flag=0;
		ArrayList<String[]> result;
		result=clc.client.details;	
		textField.setText(result.get(0)[0]);
		textField_1.setText(result.get(0)[1]);
		textField_2.setText(result.get(0)[2]);
		textField_3.setText(result.get(0)[3]);
		textField_4.setText(result.get(0)[4]);
		textField_5.setText(result.get(0)[5]);
		if((result.get(0)[6]).equals("active")) statusBox.setSelectedIndex(0);
		else 
		{
			if((result.get(0)[6]).equals("not active")) statusBox.setSelectedIndex(1);
			else
			{
				if((result.get(0)[6]).equals("freeze")) statusBox.setSelectedIndex(2);
				else
				{
					if((result.get(0)[6]).equals("freezeUntil")) statusBox.setSelectedIndex(3);
				}
			}
		}
		
	}
}
	
